package com.qyer.search.utils;/**
 * User: Z J Wu Date: 2016/01/08 Time: 17:30 Package: com.qyer.search.utils
 */

import com.qyer.qmara.Mara;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SearchMara {

  private static final Logger LOGGER = LoggerFactory.getLogger(SearchMara.class);

  private static class InnerHolder {

    private static final SearchMara INSTANCE = new SearchMara();
  }

  private SearchMara() {
  }

  public static SearchMara getInstance() {
    return InnerHolder.INSTANCE;
  }

  private Mara mara;

  private void init() throws Exception {
    CompositeConfiguration c = new CompositeConfiguration();
    c.addConfiguration(new SystemConfiguration());
    String propertiesFile = c.getString("common.mara.file");
    if (StringUtils.isBlank(propertiesFile)) {
      throw new ConfigurationException("Basic mara file is not configured properly.");
    }
    LOGGER.info("Mara config file is \"{}\"", propertiesFile);
    c = new CompositeConfiguration();
    c.addConfiguration(new PropertiesConfiguration(propertiesFile));

    String maraURL = c.getString("mara.zk.urls");
    int maraSessionTimeout = c.getInt("mara.zk.sessionTimeout");
    String confFileRoot = c.getString("mara.conf.root.path");
    String serviceDiscoveryRoot = c.getString("mara.servdisc.root.path");
    LOGGER.info("Mara conf file root - {}", confFileRoot);
    LOGGER.info("Mara service discovery root - {}", serviceDiscoveryRoot);
    this.mara = new Mara(maraURL, maraSessionTimeout, confFileRoot, serviceDiscoveryRoot);
    LOGGER.info("Mara({}) inited.", this.mara);
  }
}
